package cn.gwm.flink.streaming.dwd.consumer;

import cn.gwm.flink.streaming.beans.dwd.DwdStandardField;
import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.constant.VehicleType;
import cn.gwm.flink.streaming.dwd.check.VehicleConsumer;
import cn.gwm.utils.ConfigLoader;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ES11Consumer
 * @Description
 * @Author LiangGuang
 * @Date 2023/05/18 11:02
 */
@Data
@Accessors(chain = true)
public class P03PhevConsumer extends VehicleConsumer {
    public static Map<String, String> BATT_SOC_MAP = new HashMap<>();

    static {
        /*
        0x0:notactive
        0x1:active
        0x2: 100%
        0x3: 50%
        0x4: 60%
        0x5: 70%
        0x6: 80%
        0x7: 90%
        0x8:Invalid
         */
        BATT_SOC_MAP.put(BaseFields.defaultVal, BaseFields.defaultVal);
        BATT_SOC_MAP.put("0", BaseFields.defaultVal);
        BATT_SOC_MAP.put("1", BaseFields.defaultVal);
        BATT_SOC_MAP.put("8", BaseFields.defaultVal);
        BATT_SOC_MAP.put("2", "100");
        BATT_SOC_MAP.put("3", "50");
        BATT_SOC_MAP.put("4", "60");
        BATT_SOC_MAP.put("5", "70");
        BATT_SOC_MAP.put("6", "80");
        BATT_SOC_MAP.put("7", "90");
    }


    public P03PhevConsumer() {
        this.topic = ConfigLoader.get("kafka.clean.topic.ods.p03");
        this.groupId = "consumer-dwd-p03phev";
        this.customType = VehicleType.POWER_TYPE_PHEV;
        this.mileage = "VehTotDistance";
        this.vehicleType = VehicleType.P03_PHEV;
        this.uidSuffix = VehicleType.P03_PHEV;
    }

    public P03PhevConsumer(String uidSuffix) {
        this.topic = ConfigLoader.get("kafka.clean.topic.ods.p03");
        this.groupId = "consumer-dwd-p03phev";
        this.customType = VehicleType.POWER_TYPE_PHEV;
        this.mileage = "VehTotDistance";
        this.vehicleType = VehicleType.P03_PHEV;
        this.uidSuffix = uidSuffix;
    }

    @Override
    protected void addStandards(List<DwdStandardField> list) {
        if (CollectionUtil.isNotEmpty(list)) {
            list.add(new DwdStandardField().setFieldName("request_time"));
            list.add(new DwdStandardField().setFieldName(BaseFields.DISCOUNT_POWER));
            list.add(new DwdStandardField().setFieldName(BaseFields.EXPENSIVE_POWER));
            list.add(new DwdStandardField().setFieldName(BaseFields.DISCOUNT_TIME));
            list.add(new DwdStandardField().setFieldName(BaseFields.EXPENSIVE_TIME));
            list.add(new DwdStandardField().setFieldName(BaseFields.INTELLGNT_CHRGN_MOD_RESP));
            list.add(new DwdStandardField().setFieldName(BaseFields.PLUG_CHRGN_MODE));
        }
    }

    @Override
    protected void additionalFieldHandle(JSONObject source) {
        if (source.containsKey(BaseFields.DISCOUNT_POWER) && BaseFields.defaultVal.equals(source.getStr(BaseFields.DISCOUNT_POWER))) {
            source.set(BaseFields.DISCOUNT_POWER, 0);
        }
        if (source.containsKey(BaseFields.EXPENSIVE_POWER) && BaseFields.defaultVal.equals(source.getStr(BaseFields.EXPENSIVE_POWER))) {
            source.set(BaseFields.EXPENSIVE_POWER, 0);
        }
        if (source.containsKey(BaseFields.DISCOUNT_TIME) && BaseFields.defaultVal.equals(source.getStr(BaseFields.DISCOUNT_TIME))) {
            source.set(BaseFields.DISCOUNT_TIME, 0);
        }
        if (source.containsKey(BaseFields.EXPENSIVE_TIME) && BaseFields.defaultVal.equals(source.getStr(BaseFields.EXPENSIVE_TIME))) {
            source.set(BaseFields.EXPENSIVE_TIME, 0);
        }
        // 处理 目标soc Tbox_BattSOCLim
        source.set("Tbox_BattSOCLim", BATT_SOC_MAP.get(source.getStr("Tbox_BattSOCLim")));
    }
}
